package org.com.ashen.web.voice.util;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES {

	/**
	 * 绠楁硶锛欰ES/ECB/PKCS5Padding
	 */
	public final static String ALGORITHM_AEPP = "AES/ECB/PKCS5Padding";

	/**
	 * AES鍔犲瘑
	 * 
	 * @param content
	 *            鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓嫳鏂囨垨鑰呮暟瀛椼��
	 * @return 鍔犲瘑鍚庢暟鎹�
	 */
	public static byte[] encrypt(String content, String password) {
		return encrypt(content, password, "UTF-8");
	}

	/**
	 * AES鍔犲瘑
	 * 
	 * @param content
	 *            鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓嫳鏂囨垨鑰呮暟瀛椼��
	 * @param charset
	 *            缂栫爜[UTF-8,GBK......]
	 * @return 鍔犲瘑鍚庢暟鎹�
	 */
	public static byte[] encrypt(String content, String password, String charset) {
		try {
			return encrypt(content.getBytes(charset), password.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * AES鍔犲瘑
	 * 
	 * @param content
	 *            鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @return 鍔犲瘑鍚庢暟鎹�
	 */
	public static byte[] encrypt(byte[] content, byte[] password) {
		return encrypt(content, password, ALGORITHM_AEPP);
	}

	/**
	 * AES瑙ｅ瘑
	 * 
	 * @param content
	 *            鍔犲瘑鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓嫳鏂囨垨鑰呮暟瀛椼��
	 * @return 瑙ｅ瘑鍚庢暟鎹�
	 */
	public static byte[] decrypt(byte[] content, String password) {
		return decrypt(content, password, "UTF-8");
	}

	/**
	 * AES瑙ｅ瘑
	 * 
	 * @param content
	 *            鍔犲瘑鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓嫳鏂囨垨鑰呮暟瀛椼��
	 * @param charset
	 *            缂栫爜[UTF-8,GBK......]
	 * @return 瑙ｅ瘑鍚庢暟鎹�
	 */
	public static byte[] decrypt(byte[] content, String password, String charset) {
		try {
			return decrypt(content, password.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * AES瑙ｅ瘑
	 * 
	 * @param content
	 *            鍔犲瘑鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @return 瑙ｅ瘑鍚庢暟鎹�
	 */
	public static byte[] decrypt(byte[] content, byte[] password) {
		return decrypt(content, password, ALGORITHM_AEPP);
	}

	/**
	 * AES鍔犲瘑
	 * 
	 * @param content
	 *            鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @param algorithm
	 *            绠楁硶
	 * @return 鍔犲瘑鍚庢暟鎹�
	 */
	public static byte[] encrypt(byte[] content, byte[] password, String algorithm) {
		if (content == null || password == null) {
			return null;
		}
		try {
			Cipher cipher = null;
			if (algorithm.endsWith("PKCS7Padding")) {
				cipher = Cipher.getInstance(algorithm, "BC");
			} else {
				cipher = Cipher.getInstance(algorithm);
			}
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(password, "AES"));
			return cipher.doFinal(content);
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * AES瑙ｅ瘑
	 * 
	 * @param content
	 *            鍔犲瘑鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @param algorithm
	 *            绠楁硶
	 * @return 瑙ｅ瘑鍚庢暟鎹�
	 */
	public static byte[] decrypt(byte[] content, byte[] password, String algorithm) {
		if (content == null || password == null) {
			return null;
		}
		try {
			Cipher cipher = null;
			if (algorithm.endsWith("PKCS7Padding")) {
				cipher = Cipher.getInstance(algorithm, "BC");
			} else {
				cipher = Cipher.getInstance(algorithm);
			}
			cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(password, "AES"));
			byte[] bytes = cipher.doFinal(content);
			return bytes;
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * AES鍔犲瘑
	 * 
	 * @param content
	 *            鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @param algorithm
	 *            绠楁硶
	 * @param ivStr
	 *            鍚戦噺銆�128浣嶃�戙��16涓猙yte銆�
	 * @return 鍔犲瘑鍚庢暟鎹�
	 */
	public static byte[] encrypt(byte[] content, byte[] password, byte[] ivStr, String algorithm) {
		if (content == null || password == null) {
			return null;
		}
		try {
			Cipher cipher = null;
			if (algorithm.endsWith("PKCS7Padding")) {
				cipher = Cipher.getInstance(algorithm, "BC");
			} else {
				cipher = Cipher.getInstance(algorithm);
			}
			IvParameterSpec iv = new IvParameterSpec(ivStr);
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(password, "AES"), iv);
			return cipher.doFinal(content);
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
	}

	/**
	 * AES瑙ｅ瘑
	 * 
	 * @param content
	 *            鍔犲瘑鍐呭
	 * @param password
	 *            瀵嗛挜銆�128浣嶃�戙��16涓猙yte銆�
	 * @param algorithm
	 *            绠楁硶
	 * @param ivStr
	 *            鍚戦噺銆�128浣嶃�戙��16涓猙yte銆�
	 * @return 瑙ｅ瘑鍚庢暟鎹�
	 */
	public static byte[] decrypt(byte[] content, byte[] password, byte[] ivStr, String algorithm) {
		if (content == null || password == null) {
			return null;
		}
		try {
			Cipher cipher = null;
			if (algorithm.endsWith("PKCS7Padding")) {
				cipher = Cipher.getInstance(algorithm, "BC");
			} else {
				cipher = Cipher.getInstance(algorithm);
			}
			IvParameterSpec iv = new IvParameterSpec(ivStr);
			cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(password, "AES"), iv);
			byte[] bytes = cipher.doFinal(content);
			return bytes;
		} catch (Exception e) {
			throw new IllegalArgumentException(e);
		}
	}

}
